﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BudgetApp.Adapters;
using BudgetApp.Data;
using BudgetApp.Data.Models;
using HelpDeskTicketingApp.Models;

namespace BudgetApp.Controllers
{
    [Authorize]
    public class SpendingController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        private IBudgetAdapter _budgetAdapter;

        public SpendingController()
        {
            _budgetAdapter = BudgetAdapterFactory.GetBudgetAdapter();
        }

        public SpendingController(IBudgetAdapter budgetAdapter)
        {
            _budgetAdapter = budgetAdapter;
        }

        // GET: Spending
        public ActionResult Index()
        {
            var trackSpends = db.TrackSpends.Include(t => t.Category).Include(t => t.InputType).Include(t => t.SubCategory).Include(t => t.User);
            return View(trackSpends.ToList());
        }

        // GET: Spending/Details/id
        public ActionResult Details(int id)
        {
            var trackSpend = _budgetAdapter.GetSpendDetails(id);
            if (trackSpend == null)
            {
                return HttpNotFound();
            }
            return View(trackSpend);
        }

        // GET: Spending/Create
        public ActionResult Create()
        {
            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType");
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type");
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType");
            return View();
        }

        // POST: Spending/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "SpendId,DateSpent,Amount,Where,Notes,CategoryType,SubCategoryType,Type,UserId")] TrackSpend trackSpend)
        {
            if (ModelState.IsValid)
            {
                db.TrackSpends.Add(trackSpend);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType", trackSpend.CategoryType);
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type", trackSpend.Type);
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType", trackSpend.SubCategoryType);
            return View(trackSpend);
        }

        // GET: Spending/Edit/id
        public ActionResult Edit(int id)
        {
            var trackSpend = _budgetAdapter.GetSpendDetails(id);
            if (trackSpend == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType");
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type");
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType");
            return View(trackSpend);
        }

        // POST: Spending/Edit/id
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "SpendId,DateSpent,Amount,Where,Notes,CategoryType,SubCategoryType,Type,UserId")] TrackSpend trackSpend)
        {
            if (ModelState.IsValid)
            {
                db.Entry(trackSpend).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType", trackSpend.CategoryType);
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type", trackSpend.Type);
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType", trackSpend.SubCategoryType);
            return View(trackSpend);
        }

        // GET: Spending/Delete/id
        public ActionResult Delete(int id)
        {
            var trackSpend = _budgetAdapter.GetSpendDetails(id);
            if (trackSpend == null)
            {
                return HttpNotFound();
            }
            return View(trackSpend);
        }

        // POST: Spending/Delete/id
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var trackSpend = _budgetAdapter.GetSpendDetails(id);
            db.TrackSpends.Remove(trackSpend);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
